const stringifyForLog = (value, max = 800) => {
  if (value === undefined || value === null) {
    return value;
  }
  if (typeof value === 'string') {
    return value.length > max ? `${value.slice(0, max)}...` : value;
  }
  try {
    const str = JSON.stringify(value);
    return str.length > max ? `${str.slice(0, max)}...` : str;
  } catch (error) {
    return String(value);
  }
};

export const createDefaultTool = (toolName) => ({
  name: toolName,
  async doCall({ mcpClient, args, toolName: name }) {
    console.log('[Tools][default] doCall args', { toolName: name, args: stringifyForLog(args) });
    const result = await mcpClient.callTool(name, args);
    console.log('[Tools][default] doCall result', stringifyForLog(result));
    return result;
  },
  async getToolRawResult(callResult) {
    console.log('[Tools][default] getToolRawResult input', stringifyForLog(callResult));
    return callResult;
  },
  async getToolTextResult(rawResult) {
    console.log('[Tools][default] getToolTextResult input', stringifyForLog(rawResult));
    return rawResult;
  }
});

